/*******************************************************************
  File Name      : WF5803
  Description       :
  Created on: 2019-1-14
  Author: Admin
********************************************************************/
#include "WF5803.h"
#include <string.h>
#include <math.h>

long reading = 0;
float fDat = 0;
float Press_Data = 0;
float Temp_Data = 0;
void WF5803_I2C_ReadNByte(u8 DeviceAddr, u8 RegAddr, u8 *pBuf, u8 num);
void WF5803_I2C_WriteCMD(u8 DeviceAddr, u8 RegAddr, u8 CMD);

void WF5803_I2C_WriteCMD(u8 DeviceAddr, u8 RegAddr, u8 CMD)
{
  I2C_HAL_GenerateStart();
  I2C_HAL_SendByte(DeviceAddr & 0XFE);
  I2C_HAL_SendByte(RegAddr);
  I2C_HAL_SendByte(CMD);
  I2C_HAL_GenerateStop();
}

#define SCALE_FACTOR 10 // 缩放因子，保留一位小数
// void CalSensorData(int32_t* pressure_int, int16_t* temperature_int)
// {
//     uint8_t RawData[5];
//     memset(RawData, 0, sizeof(RawData));

//     // 采集温度数据
//     WF5803_I2C_WriteCMD(WF5803IICADDR, 0x30, 0x08);
//     TIM_Delay_ms(10);
//     // 采集气压数据
//     WF5803_I2C_WriteCMD(WF5803IICADDR, 0x30, 0x09);
//     TIM_Delay_ms(10);

//     // 读取数据
//     GrokWF5803_I2C_ReadNByte(WF5803IICADDR, 0x06, RawData, 5);

//     // 处理气压
//     uint32_t pressure_raw = ((uint32_t)RawData[0] << 16) | ((uint32_t)RawData[1] << 8) | RawData[2];
//     int32_t signed_pressure = (int32_t)((pressure_raw << 8) >> 8);
//     float fDat = (float)signed_pressure / 8388608.0f;
//     float pressure_float = 125.0f * (fDat - 0.1f) + 30.0f;
//     if (pressure_int != NULL)
//     {
//         *pressure_int = (int32_t)(pressure_float * SCALE_FACTOR);
//     }

//     // 处理温度
//     uint16_t temperature_raw = ((uint16_t)RawData[3] << 8) | RawData[4];
//     int16_t signed_temperature = (int16_t)temperature_raw;
//     float temperature_float = (float)signed_temperature / 256.0f;
//     if (temperature_int != NULL)
//     {
//         *temperature_int = (int16_t)(temperature_float * SCALE_FACTOR);
//     }
// }

void CalSensorData(float* pressure, float* temperature)
{
    uint8_t RawData[5];
    static uint32_t pressure_reading;
    uint16_t temperature_reading;
    static int32_t signed_pressure;
    int16_t signed_temperature;
    static float fDat;

    // 清空 RawData 数组
    memset(RawData, 0, sizeof(RawData));

    // 步骤 1：采集温度数据
    WF5803_I2C_WriteCMD(WF5803IICADDR, 0x30, 0x08);
    TIM_Delay_ms(10);

    // 步骤 2：采集气压数据
    WF5803_I2C_WriteCMD(WF5803IICADDR, 0x30, 0x09);
    TIM_Delay_ms(10);

    // 步骤 3：读取气压和温度数据
    GrokWF5803_I2C_ReadNByte(WF5803IICADDR, 0x06, RawData, 5);
    
    // 处理气压数据
    pressure_reading = ((uint32_t)RawData[0] << 16) | ((uint32_t)RawData[1] << 8) | RawData[2];
    signed_pressure = (int32_t)(pressure_reading << 8) >> 8;
    fDat = (float)signed_pressure / 8388608.0f;
    if (pressure != NULL) {
        *pressure = 125.0f * (fDat - 0.1f) + 30.0f;
    }
    // 处理温度数据
    temperature_reading = ((uint16_t)RawData[3] << 8) | RawData[4];
    signed_temperature = (int16_t)temperature_reading;
    if (temperature != NULL) {
        *temperature = (float)signed_temperature / 256.0f;
    }
}


void GrokWF5803_I2C_ReadNByte(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t *pBuf, uint8_t num)
{
    uint8_t n;
    
    I2C_HAL_GenerateStart();
    I2C_HAL_SendByte(DeviceAddr & 0xFE);    // 发送写地址
    I2C_HAL_SendByte(RegAddr);              // 发送寄存器地址
    I2C_HAL_GenerateStart();                // 重启
    I2C_HAL_SendByte(DeviceAddr | 0x01);    // 发送读地址
    
    for (n = 0; n < num; n++)
    {
        uint8_t ack = (n < num - 1) ? I2C_ACK : I2C_NACK;
        I2C_HAL_ReadByte(&pBuf[n], ack);
    }
    
    I2C_HAL_GenerateStop();
}



